<?php
/**
 * User: 1971229701@qq.com
 * Date: 2020/11/2/002
 * Time: 10:48
 */

namespace app\admin\controller;


use app\common\model\Menu;
use think\facade\Env;

class Api extends BaseAdmin
{
    public function index()
    {
        $homeInfo = [
            //'title' => '首页',
            'title' => '管理员管理',
            //'href' => 'index/welcome',
            'href' => 'admin/listing',
        ];
        $logoInfo = [
            'title' => '中诚汽车',
            'href' => 'javascript:;',
            'image' => "/static/admin/images/logo.png",
        ];
        $menuInfo = $this->getMenuList();
        $systemInit = [
            'homeInfo' => $homeInfo,
            'logoInfo' => $logoInfo,
            'menuInfo' => $menuInfo,
        ];
        return json($systemInit);
    }

//    protected function getMenuList()
//    {
//        $list = [
//            ['title' => '常规管理',
//                'href' => '',
//                'target' => '_self',
//                'child' => [
//                    ['title' => '管理员管理',
//                        'href' => '',
//                        'icon' => 'fa fa-home',
//                        'target' => '_self',
//                        'child' => [
//                            [
//                                'title' => '管理员列表',
//                                'href' => 'www.baidu.com',
//                                'icon' => 'fa fa-tachometer',
//                                'target' => '_self',
//                            ]
//                        ]
//                    ],
//                ],
//            ]
//        ];
//        return $list;
//    }

    protected function getMenuList()
    {
        $menuList = Menu::
        field('id,pid,title,icon,href,target')
            ->select();
        $menuList = $this->buildMenuChild(0, $menuList);
        return $menuList;
    }

    //递归获取子菜单
    private function buildMenuChild($pid, $menuList)
    {
        $treeList = [];
        foreach ($menuList as $v) {
            if ($pid == $v['pid']) {
                $node = $v;
                $child = $this->buildMenuChild($v['id'], $menuList);
                if (!empty($child)) {
                    $node['child'] = $child;
                }
                // todo 后续此处加上用户的权限判断
                $treeList[] = $node;
            }
        }
        return $treeList;
    }
}